// detail-user.ts - 用户端任务详情页
interface TaskData {
  id: string
  type: string
  typeIcon: string
  typeName: string
  startLocation: string
  startAddress?: string
  endLocation: string
  endAddress?: string
  status: string
  statusText: string
  urgencyLevel: string
  urgencyText: string
  passengerCount: number
  createTime: string
  contactPhone: string
  canCancel?: boolean
  distance: number
  duration: number
  patientCondition?: string
  orderType?: string
}

Page({
  data: {
    taskId: '',
    statusBarHeight: '44px',
    taskData: null as TaskData | null,
    showMap: false,
    currentLocation: null as any,
    mapMarkers: [] as any[],
    uploadedImages: [] as string[]
  },

  onLoad(options: any) {
    this.setStatusBarHeight()
    this.checkLogin()
    
    const { id } = options
    if (id) {
      this.setData({ taskId: id })
      this.loadTaskDetail()
    }
  },

  setStatusBarHeight() {
    const windowInfo = (wx as any).getWindowInfo()
    const statusBarHeight = windowInfo.statusBarHeight || 44
    this.setData({
      statusBarHeight: statusBarHeight + 'px'
    })
  },

  checkLogin() {
    const userRole = wx.getStorageSync('userRole')
    if (userRole !== 'user') {
      wx.reLaunch({ url: '/pages/login/login' })
    }
  },

  goBack() {
    wx.navigateBack()
  },

  loadTaskDetail() {
    wx.showLoading({ title: '加载中...' })
    
    setTimeout(() => {
      const mockData: TaskData = {
        id: this.data.taskId,
        type: 'ambulance',
        typeIcon: '🚑',
        typeName: '救护车',
        startLocation: '辽宁省-沈阳市',
        startAddress: '某某东大街112号院东恒生宇楼B座11.5',
        endLocation: '北京市-东城区',
        endAddress: '协和医院本部',
        status: 'in_progress',
        statusText: '已到达',
        urgencyLevel: 'low',
        urgencyText: '低',
        passengerCount: 2,
        createTime: '2025-09-07 周日 09:22',
        contactPhone: '135 0000 0000',
        canCancel: false,
        distance: 680,
        duration: 450,
        patientCondition: '123',
        orderType: '本人下单'
      }
      
      const mockImages = [
        'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_154f5afcc73ab7814a745378c6dd2846_edit_391954352758421.png',
        'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_06409c288f6c65c4f600bddf9283602f_edit_391871265051142.png',
        'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/art_works/share_0e5b4a2b28ed3de4b0bd82dbf7c6903d_edit_391867197811038.png'
      ]
      
      this.setData({
        taskData: mockData,
        showMap: mockData.status === 'in_progress',
        uploadedImages: mockImages,
        currentLocation: {
          latitude: 39.908823,
          longitude: 116.397470
        }
      })
      
      this.updateMapMarkers()
      wx.hideLoading()
    }, 800)
  },

  updateMapMarkers() {
    if (!this.data.currentLocation) return
    
    const markers = [{
      id: 1,
      latitude: this.data.currentLocation.latitude,
      longitude: this.data.currentLocation.longitude,
      width: 40,
      height: 40,
      label: {
        content: '🚑',
        fontSize: 24,
        color: '#FF5252',
        bgColor: '#ffffff',
        borderRadius: 20,
        padding: 5
      }
    }]
    
    this.setData({ mapMarkers: markers })
  },

  contactService() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567'
    })
  },

  cancelTask() {
    wx.showModal({
      title: '确认取消',
      content: '确定要取消这个救援申请吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '已取消申请',
            icon: 'success'
          })
          setTimeout(() => wx.navigateBack(), 1500)
        }
      }
    })
  },

  showMore() {
    wx.showActionSheet({
      itemList: ['联系客服', '分享任务'],
      success: (res) => {
        if (res.tapIndex === 0) {
          this.contactService()
        }
      }
    })
  },

  previewImage(e: any) {
    const index = e.currentTarget.dataset.index
    wx.previewImage({
      current: this.data.uploadedImages[index],
      urls: this.data.uploadedImages
    })
  }
})

